package it.creazionicreative.engine;

import it.creazionicreative.Const;
import it.creazionicreative.Utils;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class TTSEngine extends BaseEngine {

	private Intent checkttsintent;
	private TextToSpeech tts;
	private boolean showtoast = false;

	public TTSEngine(Activity activity) {
		super(activity, Const.ENGINE_TTS);
	}


	@Override
	public void Init() {

		checkengine();
		super.Init();
	}

	@Override
	public void Shutdown() {
		if (tts != null)
		{

			tts.stop();
			tts.shutdown();
		}
		super.Shutdown();
	}


	public void InitDownloadTTSData()
	{
		Log.i(Const.ENGINE_TTS, "TTS NOT Installed");	
		// missing data, install it
		Intent installIntent = new Intent();
		installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
		getappActivity().startActivity(installIntent);
	}


	public void Speak(String text)
	{
		tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
		if (showtoast)
			Utils.toast(getappActivity(), text);
	}


	private void checkengine()
	{
		checkttsintent = new Intent();
		checkttsintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);			
		getappActivity().startActivityForResult(checkttsintent, Const.TTS_CHECK_ENGINE);
	}


	public void InitTTS()
	{
		tts = new TextToSpeech(getappActivity(), new OnInitListener() {

			public void onInit(int status) {
				if (status == TextToSpeech.SUCCESS)
				{
					Speak("Hello Tommy, Welcome back!");
				}

			}
		});

		if (tts.isLanguageAvailable(Locale.ITALY) == TextToSpeech.LANG_MISSING_DATA)
		{
			Utils.alertbox(getappActivity(), "SquidAI", "Italian language not found!!");			
		}
		else
		{
			tts.setLanguage(Locale.ITALY);
		}

	}
}
